package hongcaosp.app.android.login;

import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.UserInfoWrap;
import hongcaosp.app.android.modle.impl.LoginModleImpl;
import hongcaosp.app.android.modle.impl.PasswordModleImpl;
import hongcaosp.app.android.modle.mi.LoginModle;
import hongcaosp.app.android.modle.mi.PasswordModle;
import xlj.lib.android.base.component.BasePresenter;
import xlj.lib.android.base.component.IView;
import xlj.lib.android.base.toastcompat.ToastManager;

public class LoginPresenter extends BasePresenter {
    private ICodeView codeView;
    private ILoginView loginView;
    private PasswordModle passwordModle;
    private LoginModle loginModle;

    public LoginPresenter(ILoginView loginView) {
        this.loginView = loginView;
        loginModle = new LoginModleImpl();
    }

    public LoginPresenter(ICodeView codeView, ILoginView loginView) {
        this.codeView = codeView;
        this.loginView = loginView;
        loginModle = new LoginModleImpl();
        passwordModle = new PasswordModleImpl();
    }

    public void sendCode(String mobile) {
        passwordModle.sendCode(mobile, 1, new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                codeView.onCodeSend(true);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                codeView.onCodeSend(false);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    public void sendPasswordCode(String mobile) {
        passwordModle.sendCode(mobile, 2, new DataCallBack() {
            @Override
            public void onGetData(Object data) {
                super.onGetData(data);
                codeView.onCodeSend(true);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                codeView.onCodeSend(false);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }

    public void loginWithCode(String mobile,String code) {
        loginModle.login(mobile, code, null, null, 0, new DataCallBack<UserInfoWrap>() {
            @Override
            public void onGetData(UserInfoWrap data) {
                super.onGetData(data);
                if (data.getUserInfo() != null) {
                    UserToken.getDefault().setUser(data.getUserInfo());

                    loginView.onLogged(true,null);
                } else {
                    loginView.onLogged(false,"登录失败");
                }

            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                loginView.onLogged(false,errorMessage);
            }
        });
    }

    public void loginWithPassword(String mobile,String password) {
        loginModle.login(mobile, null, password, null, 1, new DataCallBack<UserInfoWrap>() {
            @Override
            public void onGetData(UserInfoWrap data) {
                super.onGetData(data);
                if (data.getUserInfo() != null) {
                    UserToken.getDefault().setUser(data.getUserInfo());
                    loginView.onLogged(true,null);
                } else {
                    loginView.onLogged(false,"登录失败");
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                loginView.onLogged(false,errorMessage);
            }
        });
    }

    public void loginWithWechat(String code) {
        loginModle.login(null, null, null, code, 2, new DataCallBack<UserInfoWrap>() {
            @Override
            public void onGetData(UserInfoWrap data) {
                super.onGetData(data);
                if (data.getUserInfo() != null) {
                    UserToken.getDefault().setUser(data.getUserInfo());
                    loginView.onLogged(true,null);
                } else {
                    loginView.onLogged(false,"登录失败");
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                loginView.onLogged(false,errorMessage);
            }
        });
    }

    public void updatePassword(String mobile,String verificationCode,String password) {
        passwordModle.updatePassword(mobile, verificationCode, password, new DataCallBack<UserInfoWrap>() {
            @Override
            public void onGetData(UserInfoWrap data) {
                super.onGetData(data);
                if (data != null && data.getUserInfo() != null) {
                    UserToken.getDefault().setUser(data.getUserInfo());
                    loginView.onLogged(true,null);
                } else {
                    loginView.onLogged(false,"密码修改失败");
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                loginView.onLogged(false,errorMessage);
            }
        });
    }
}
